package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.ScriptRankResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

public class RankScriptItemAdp extends RecyclerView.Adapter {

    private Context mContext;
    private List<ScriptRankResponse.Data.Script> mScriptList;
    private RecyclerView mRecyclerView;
    private int star;

    public RankScriptItemAdp(Context mContext, List<ScriptRankResponse.Data.Script> mScriptList) {
        this.mContext = mContext;
        this.mScriptList = mScriptList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_script, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        ScriptRankResponse.Data.Script script = mScriptList.get(position);

        vh.tvScriptName.setText(script.getName());
        vh.tvScore.setText(script.getScore());
        vh.tvScriptIntro.setText(script.getIntro());

        float rate = Float.parseFloat(script.getScore());
        vh.rating.setMax(10);
        star = ((int) rate) / 2;
        vh.rating.setRating(star);

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                vh.ivScriptCover, script.getCover(), R.mipmap.ic_test_bg, 0);

        holder.itemView.setOnClickListener(view -> {
            String linkUrl = "http://h5.hqzl.icu/#/?id=" + script.getScript_issue_id();
            ARouter.getInstance().build(ArouterAppPath.WEBVIEW_ACTIVITY).withString("linkUrl", linkUrl).navigation();
        });
    }

    @Override
    public int getItemCount() {
        return mScriptList.size();
    }

    /**
     * 绑定recycleview
     *
     * @param recyclerView
     */
    public void bindRecycleView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        recyclerView.setAdapter(this);
    }

    /**
     * 更新信息
     */
    public void updateScriptList(List<ScriptRankResponse.Data.Script> scripts) {
        mScriptList.clear();
        mScriptList.addAll(scripts);
        if (mRecyclerView != null) {
            notifyDataSetChanged();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        QMUIRadiusImageView ivScriptCover;
        TextView tvScriptName, tvScriptIntro, tvScore;
        RatingBar rating;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivScriptCover = itemView.findViewById(R.id.ivScriptCover);
            tvScriptName = itemView.findViewById(R.id.tvScriptName);
            tvScriptIntro = itemView.findViewById(R.id.tvScriptIntro);
            tvScore = itemView.findViewById(R.id.tvScore);
            rating = itemView.findViewById(R.id.rating);
        }
    }
}
